package com.cms.common.util.pass;

import java.util.HashMap;
import java.util.Map;

import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig.Feature;
import org.codehaus.jackson.map.ser.CustomSerializerFactory;
import org.codehaus.jackson.type.TypeReference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * json 工具类
 * @author Administrator
 *
 */
public class JsonUtil {
		 
	    private static final Logger log =LoggerFactory.getLogger(JsonUtil.class);
	 
	    final static ObjectMapper objectMapper;
	 
	    /**
	     * 是否打印美观格式
	     */
	    static boolean isPretty = false;
	 
	    static {
	    	objectMapper = new ObjectMapper();
			objectMapper.configure(Feature.FAIL_ON_EMPTY_BEANS, false);
			CustomSerializerFactory sf = new CustomSerializerFactory();
			objectMapper.setSerializerFactory(sf);
	    }
	 
	    public static ObjectMapper getObjectMapper() {
	        return objectMapper;
	    }
	    /**
	     * JSON串转换为Java泛型对象，可以是各种类型，此方法最为强大。用法看测试用例。
	     * @param <T>
	     * @param jsonString JSON字符串
	    * @param tr TypeReference,例如: new TypeReference< List<FamousUser> >(){}
	     * @return List对象列表
	     */
	    public static <T> T json2GenericObject(String jsonString, TypeReference<T> tr) {
	 
	        if (jsonString == null || "".equals(jsonString)) {
	            return null;
	        } else {
	            try {
	                return (T) objectMapper.readValue(jsonString, tr);
	            } catch (Exception e) {
	                log.warn("json error:" + e.getMessage());
	            }
	        }
	        return null;
	    }
	 
	    /**
	     * Java对象转Json字符串
	     * 
	     * @param object Java对象，可以是对象，数组，List,Map等
	     * @return json 字符串
	     */
	    public static String toJson(Object object) {
	        String jsonString = "";
	        try {
	            if (isPretty) {
	                jsonString = objectMapper.defaultPrettyPrintingWriter().writeValueAsString(object);
	            } else {
	                jsonString = objectMapper.writeValueAsString(object);
	            }
	        } catch (Exception e) {
	            e.printStackTrace();
	        	log.error("json error:" + e.getMessage());
	        }
	        return jsonString;
	 
	    }
	 
	    /**
	     * Json字符串转Java对象
	     * 
	     * @param jsonString
	     * @param c
	     * @return
	     */
	    public static Object json2Object(String jsonString, Class<?> c) {
	 
	        if (jsonString == null || "".equals(jsonString)) {
	            return "";
	        } else {
	            try {
	                return objectMapper.readValue(jsonString, c);
	            } catch (Exception e) {
	                log.warn("json error:" + e.getMessage());
	            }
	 
	        }
	        return "";
	    }
	    
	    public static void main(String[] args) {
	    	Map<String,Object> group = new HashMap<String,Object>();
	        Map<String,Object> nameObj = new HashMap<String,Object>();
	        group.put("name", "你好");
	        String post = JsonUtil.toJson(group);
	        System.out.println(post);
	        
		}
	 
}
